#include "pwm.h"

/* 频率范围1Hz - 6.5kHz */
#define pwm3_path "/sys/class/pwm/pwmchip2/"
#define pwm7_path "/sys/class/pwm/pwmchip6/"

extern sem_t sem_pwm;
extern int pid_out;

void pwm_init(void)
{
    int fd1;
    int fd2;
    int fd1_period;
    int fd2_period;
    /********************************* PWM3 ******************************/
    fd1 = open(pwm3_path"export", O_WRONLY);
    if( fd1 < 0 ){
        printf("pwm2 export file open error!\n");
        close(fd1);
        exit(-1);
    }
    write(fd1, "0", 1);
    /***/
    fd1_period = open(pwm3_path"pwm0/period", O_WRONLY);
    if( fd1_period < 0 ){
        printf("pwm2 period file open error!\n");
        close(fd1);
        exit(-1);
    }
    write(fd1_period, "1000000", 7);
    /***/
    fd1 = open(pwm3_path"pwm0/duty_cycle", O_WRONLY);
    if( fd1 < 0 ){
        printf("pwm2 duty_cycle file open error!\n");
        close(fd1);
        exit(-1);
    }
    write(fd1, "2000", 4);
    /***/
    fd1 = open(pwm3_path"pwm0/polarity", O_WRONLY);
    if( fd1 < 0 ){
        printf("pwm3 polarity file open error!\n");
        close(fd1);
        exit(-1);
    }
    write(fd1, "normal", 6);
    /***/
    fd1 = open(pwm3_path"pwm0/enable", O_WRONLY);
    if( fd1 < 0 ){
        printf("pwm3 enable file open error!\n");
        close(fd1);
        exit(-1);
    }
    write(fd1, "1", 1);
    /*********************************************************************/

    /********************************* PWM6 ******************************/
    fd2 = open(pwm7_path"export", O_WRONLY);
    if( fd2 < 0 ){
        printf("pwm7 export file open error!\n");
        close(fd2);
        exit(-1);
    }
    write(fd2, "0", 1);
    /***/
    fd2_period = open(pwm7_path"pwm0/period", O_WRONLY);
    if( fd2_period < 0 ){
        printf("pwm7 period file open error!\n");
        close(fd2);
        exit(-1);
    }
    write(fd2_period, "1000000", 7);
    /***/
    fd2 = open(pwm7_path"pwm0/duty_cycle", O_WRONLY);
    if( fd2 < 0 ){
        printf("pwm7 duty_cycle file open error!\n");
        close(fd2);
        exit(-1);
    }
    write(fd2, "2000", 4);
    /***/
    fd2 = open(pwm7_path"pwm0/polarity", O_WRONLY);
    if( fd2 < 0 ){
        printf("pwm7 polarity file open error!\n");
        close(fd2);
        exit(-1);
    }
    write(fd2, "normal", 6);
    /***/
    fd2 = open(pwm7_path"pwm0/enable", O_WRONLY);
    if( fd2 < 0 ){
        printf("pwm7 enable file open error!\n");
        close(fd2);
        exit(-1);
    }
    write(fd2, "1", 1);
    /*********************************************************************/
    write(fd1, "0", 1);
    write(fd2, "0", 1);
    
    int freq = 0;
    char str[20];

    while(1){
        // printf("please enter freq:\n");
        // scanf("%d",&freq);
        sem_wait(&sem_pwm);
        if( pid_out != 0 ){
            //转换为ns
            freq = (1/(pid_out*1.0))*1000*1000*1000;
            sprintf(str, "%d", freq);
            write(fd1_period, str, strlen(str));
            write(fd2_period, str, strlen(str));
            write(fd1, "1", 1);
            write(fd2, "1", 1);
            // printf(" = %d ns\n",freq);
            
        }
        else{
            write(fd1, "0", 1);
            write(fd2, "0", 1);
        }
        sem_post(&sem_pwm);
        usleep(1*1000);
    }
}